package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysTagBo;
import com.xmtlxx.system.domain.vo.SysTagVo;
import com.xmtlxx.system.service.ISysTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 标签管理Controller
 *
 * @author xmtlxx
 * @date 2022-04-26
 */
@Validated
@Api(value = "标签管理控制器", tags = {"标签管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysTag")
public class SysTagController extends BaseController {

    private final ISysTagService iSysTagService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysTagVo> initBean(){
        SysTagVo vo = new SysTagVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询标签管理列表
     */
    @ApiOperation("查询标签管理列表")
//    @SaCheckPermission("system:sysTag:list")
    @GetMapping("/list")
    public TableDataInfo<SysTagVo> list(@Validated(QueryGroup.class) SysTagBo bo, PageQuery pageQuery) {
        return iSysTagService.queryPageList(bo, pageQuery);
    }

    /**
     * 标签列表
     */
    @ApiOperation("标签列表")
//    @SaCheckPermission("datas:query:tag")
    @GetMapping("/options")
    public R<List<SysTagVo>> options(SysTagBo bo) {
        return R.ok(iSysTagService.queryList(bo));
    }

    /**
     * 导出标签管理列表
     */
    @ApiOperation("导出标签管理列表")
    @SaCheckPermission("system:sysTag:export")
    @Log(title = "标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysTagBo bo, HttpServletResponse response) {
        List<SysTagVo> list = iSysTagService.queryList(bo);
        ExcelUtil.exportExcel(list, "标签管理", SysTagVo.class, response);
    }

    /**
     * 获取标签管理详细信息
     */
    @ApiOperation("获取标签管理详细信息")
    @SaCheckPermission("system:sysTag:query")
    @GetMapping("/{tagId}")
    public R<SysTagVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("tagId") String tagId) {
        return R.ok(iSysTagService.queryById(tagId));
    }

    /**
     * 新增标签管理
     */
    @ApiOperation("新增标签管理")
    @SaCheckPermission("system:sysTag:add")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTagVo vo) {
        SysTagBo bo = new SysTagBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysTagService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改标签管理
     */
    @ApiOperation("修改标签管理")
    @SaCheckPermission("system:sysTag:edit")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTagVo vo) {
        SysTagBo bo = new SysTagBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysTagService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除标签管理
     */
    @ApiOperation("删除标签管理")
    @SaCheckPermission("system:sysTag:remove")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagIds}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] tagIds) {
        return toAjax(iSysTagService.deleteWithValidByIds(Arrays.asList(tagIds), true) ? 1 : 0);
    }
}
